package at.fhooe.mcm.logic.event;

public class EventType
{

	// Members
	private int		m_hashCode;
	private String	m_id;

	// Methods
	public EventType(String name)
	{
		if (name == null)
		{
			throw new NullPointerException("name of entity type must not be NULL");
		}

		m_id = name;

		// use only lower case letters to generate hashcode to avoid misspelled
		// entity type names
		m_hashCode = name.toLowerCase().hashCode();
	}

	public int getId()
	{
		return m_hashCode;
	}

	public String getName()
	{
		return m_id;
	}

	@Override
	public int hashCode()
	{
		return m_hashCode;
	}

	@Override
	public boolean equals(Object _obj)
	{
		if (this == _obj)
		{
			return true;
		}

		if (_obj == null)
		{
			return false;
		}

		if (getClass() != _obj.getClass())
		{
			return false;
		}

		EventType other = (EventType) _obj;
		if (m_hashCode != other.hashCode())
		{
			return false;
		}

		// also very unlikely equal strings could result in the same hash code,
		// let's test this only in 'debug' mode
		assert (equalNames(other));

		return true;
	}

	private boolean equalNames(EventType other)
	{
		if (m_id == null)
		{
			if (other.m_id != null)
			{
				return false;
			}
		}

		return m_id.equals(other.m_id);
	}
}
